package com.example.tool.util.ems;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Map;
import java.util.TreeMap;

public class SignUtil {
    /**
     * 生成EMS API签名
     * @param params 所有请求参数
     * @param appSecret 应用密钥
     * @return 签名
     */
    public static String generateSign(Map<String, String> params, String appSecret) {
        // 1. 参数按key排序
        Map<String, String> sortedParams = new TreeMap<>(params);

        // 2. 拼接参数字符串
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : sortedParams.entrySet()) {
            sb.append(entry.getKey()).append(entry.getValue());
        }
        sb.append(appSecret);

        // 3. MD5加密
        return DigestUtils.md5Hex(sb.toString()).toUpperCase();
    }


    public static void main(String[] args) throws Exception {
//        URL url = new URL("https://api.ems.com.cn");
//        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
//        conn.connect();
//        System.out.println("连接成功！使用的证书：");
//        System.out.println(conn.getServerCertificates()[0]);

    }
}